<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LoginPortlet.php 406 2011-01-04 21:20:18Z steen.rabol $
 * @package Pradoportal.Porttlets.LoginPortlet
 *
 */

Prado::using('Application.Portlets.Portlet');
class LoginPortlet extends Portlet
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->Page->isPostBack)
		{
			foreach($this->Request->Cookies as $cookie)
			{
				if($cookie->Name == ($this->Application->Parameters['CookiePrefix'] . "_loginuname"))
				{
					$this->Username->Text 		= $cookie->Value;
					$this->RememberMe->Checked	= true;
				}
			}

			$this->Page->getClientScript()->registerFocusControl($this->Username);
		}
	}

	public function validateUserName($sender,$param)
	{
		$param->IsValid = $this->DataAccess->createCommand("select id from tblusers where name='" . $this->Username->Text . "'")->queryScalar();
	}

	public function loginButtonClicked($sender,$param)
	{
		if($this->Page->IsValid)
		{
			$authManager	= $this->Application->getModule('auth');
            if($authManager->login($this->Username->Text,$this->Password->Text))
			{
				if(TPropertyValue::ensureBoolean($this->Application->Parameters['UseCookieExpire']));
				{
					// Set the login expire cookie
					$cvalue = time() + ($this->Application->Parameters['LoginExpire'] * 60);
					$cookie = new THttpCookie($this->Application->Parameters['CookiePrefix'] . "_loginexpire",$cvalue);
					$cookie->Expire	= $cvalue;
					$this->Response->Cookies[] = $cookie;
				}

				if($this->RememberMe->Checked)
				{
					// Create the username cookie
					$cookie = new THttpCookie($this->Application->Parameters['CookiePrefix'] . "_loginuname",$this->Username->Text);
					$cookie->Expire	= time() + (60 * 60 * 24 * 365); // 1 year;
					$this->Response->Cookies[] = $cookie;
				}
				$this->Page->gotoDefaultPage();
			}
		}
        else
        {

        }
	}
}
?>